﻿using System;
using PMSytem.Windows;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.IO;
using System.Collections.ObjectModel;
using PMSystem.DataAccess.DataRepository;

namespace PMSytem.Windows.Tests
{
	public partial class ViewPhotos : ToolWindow
	{
        public ViewPhotos()
		{
			InitializeComponent();
		}
        public ViewPhotos(int id)
        {
            InitializeComponent();
            DataContext = this;
            LoadPhotos(id);

        }
        private ObservableCollection<ImageSource> datasource =
        new ObservableCollection<ImageSource>();


        private void LoadPhotos(int id)
        {
            using (var repo = new RecordItemImageRepository())
            {
                var imgList = repo.GetAllByRecordId(id);
                foreach (var img in imgList)
                {
                    ImageSource imgsrc = ByteToImage(img.ImageBinary);
                    datasource.Add(imgsrc);
                    // imagesbyteList.Add(img.ImageBinary);
                }
                PhotosView.ItemsSource = PhotoList;
            }

        }

        public ObservableCollection<ImageSource> PhotoList
        {
            get
            {
                return datasource;
            }
        }

        public static ImageSource ByteToImage(byte[] imageData)
        {
            BitmapImage biImg = new BitmapImage();
            MemoryStream ms = new MemoryStream(imageData);
            biImg.BeginInit();
            biImg.StreamSource = ms;
            biImg.EndInit();

            ImageSource imgSrc = biImg as ImageSource;

            return imgSrc;
        }

		# region OnButtonClick

		private void OnButtonClick(object sender, RoutedEventArgs e)
		{
            ColorChooser colorChooser = new ColorChooser();
            colorChooser.ToolWindowState = ToolWindowState.Maximized;
            colorChooser.StartPosition = ToolWindowStartPosition.CenterParent;
            colorChooser.Parent = this.Parent;
            colorChooser.Show();
		}
		
		# endregion
	}
}